// groupnpc.txt
// Exactly like basicnpc, with one difference. When it first spots a foe, sends
// an alert message to everyone in its group. Good for when you have a band of 
// monsters you don't want the party to be able to pick off one at a time.
// Memory Cells:
//   Cell 0 - How creature moves.
//     0 - If 0, wander randomly. 
//     1 - Stands still until a target appears.
//     2 - Completely immobile, even if target appears.
//   Cell 1,2 - Stuff done flag. If both 0, nothing. Otherwise when this 
//     is killed, set to 1. (Example: If cell 1 is 3 and cell 2 is 5, when
//     creature is killed, sets SDF(3,5) to 1.)
//   Cell 3 - Dialogue node to start with if talked to. if left at 0, this
//     character doesn't talk.
//   Cell 4 - Group creature is in.  (You don't need to set this, but it
//     makes life a bit easier.  Leave it at 0 to do nothing.)
//   Cell 5,6 - SDF to increment.
//   Cell 7 - Amount to increment by.

begincreaturescript;

variables;

short i,target;
short i_gave_alert = 0;
short last_abil_time,which_atk,trgt,firedmg,mctx,mcty,rctx,rcty,count;
short now_hp,then_hp;
short bmessage;

body;

beginstate INIT_STATE;
	if (get_memory_cell(0) == 2)
		set_mobility(ME,0);
	if (get_memory_cell(4) > 0)
		add_char_to_group(my_number(),get_memory_cell(4));
	break;

beginstate DEAD_STATE;

	set_flag(91,0,1);
	trgt = get_ran(1,1,4);
	count = 0;
	while((count <= 3) && (which_atk < 11)){
		if((get_flag((90 + trgt),0) == 0) && (trgt != 1))
			which_atk = 11;

		if(which_atk < 11){
			count = (count + 1);
			trgt = (trgt + 1);
			if(trgt >= 5)
				trgt = 1;
		}
	}
	if(which_atk == 11){
		now_hp = get_health(my_number());
		then_hp = (1000 - now_hp);
		then_hp = (then_hp / 4);
		set_flag(91,1,then_hp);
		force_view_center(24,17);
		force_instant_terrain_redraw();
		pause(3);
		set_terrain(24,17,377);
		erase_char(7);
		put_boom_on_space(24,17,0,2);
		put_boom_on_space(24,17,0,2);
		put_boom_on_space(24,17,0,2);
		force_instant_terrain_redraw();
		run_animation_sound(5);
		pause(3);

		if(trgt == 2){
			force_view_center(24,31);
			force_instant_terrain_redraw();
			pause(3);
			set_terrain(24,31,0);
			activate_hidden_group(3);
			relocate_character(8,24,31);
			then_hp = (get_flag(92,1));
			then_hp = (then_hp * -4);
			change_char_health(8,then_hp);
			put_boom_on_space(24,31,5,0);
			force_instant_terrain_redraw();
			run_animation_sound(52);
		}
		if(trgt == 3){
			force_view_center(17,24);
			force_instant_terrain_redraw();
			pause(3);
			set_terrain(17,24,0);
			activate_hidden_group(4);
			relocate_character(9,17,24);
			then_hp = (get_flag(93,1));
			then_hp = (then_hp * -4);
			change_char_health(9,then_hp);
			put_effect_on_space(17,24,13,4,1);
			force_instant_terrain_redraw();
			run_animation_sound(17);
		}
		if(trgt == 4){
			force_view_center(31,24);
			force_instant_terrain_redraw();
			pause(3);
			set_terrain(31,24,0);
			activate_hidden_group(5);
			relocate_character(10,31,24);
			then_hp = (get_flag(94,1));
			then_hp = (then_hp * -4);
			change_char_health(10,then_hp);
			put_boom_on_space(31,24,7,0);
			force_instant_terrain_redraw();
			run_animation_sound(71);
		}
		pause(3);
		end();
	}
	else{
		erase_char(my_number());
		set_terrain(24,17,270);
		set_terrain(24,31,270);
		set_terrain(17,24,270);
		set_terrain(31,24,270);
		set_terrain(24,24,0);
		put_boom_on_space(24,17,3,0);
		put_boom_on_space(24,31,3,0);
		put_boom_on_space(17,24,3,0);
		put_boom_on_space(31,24,3,0);
		put_boom_on_space(24,24,3,0);
		activate_hidden_group(6);
		relocate_character(11,24,24);
		force_instant_terrain_redraw();
		run_animation_sound(165);
		pause(5);

		reset_dialog();
		add_dialog_str(0,"Hunh?  What's going on?  The Immortal-- he's warped over to the central platform where a black spire once was.  The statues are gone, too.",0);
		add_dialog_str(1,"But the way it all changed feels...  It feels like the time that Hunter disrupted everything in those fields.",0);
		add_dialog_str(2,"_You...  you...  fight well.  Good.  I like a challenge._",0);
		add_dialog_str(3,"The Immortal seems exhausted.  He must have been exhausting himself, keeping up an illusion that compelling for so long.",0);
		add_dialog_str(4,"_It's not...  not over!  You have yet to feel...  my...  true wrath!_",0);
		add_dialog_str(5,"He's panting and sweating profusely.  This should not be much of a challenge.",0);
		add_dialog_choice(0,"Thankfully.");
		bmessage = run_dialog(1);
	}

break;

beginstate START_STATE; 

	if(tick_difference(last_abil_time,get_current_tick()) >= 1){
		mctx = char_loc_x(my_number());
		mcty = char_loc_y(my_number());
		which_atk = get_ran(1,0,10);

		// FORM SHIFT
		if(which_atk == 0){
			trgt = get_ran(1,1,4);
			count = 0;
			while((count <= 3) && (which_atk < 10)){
				if((get_flag((90 + trgt),0) == 0) && (trgt != 1))
					which_atk = 10;

				if(which_atk < 10){
					count = (count + 1);
					trgt = (trgt + 1);
					if(trgt >= 5)
						trgt = 1;
				}
			}
			if(which_atk == 10){
				now_hp = get_health(my_number());
				then_hp = (1000 - now_hp);
				then_hp = (then_hp / 4);
				set_flag(91,1,then_hp);
				force_view_center(24,17);
				force_instant_terrain_redraw();
				pause(3);
				set_terrain(24,17,377);
				erase_char(7);
				put_boom_on_space(24,17,0,2);
				put_boom_on_space(24,17,0,2);
				put_boom_on_space(24,17,0,2);
				force_instant_terrain_redraw();
				run_animation_sound(5);
				pause(3);

				if(trgt == 2){
					force_view_center(24,31);
					force_instant_terrain_redraw();
					pause(3);
					set_terrain(24,31,0);
					activate_hidden_group(3);
					relocate_character(8,24,31);
					then_hp = (get_flag(92,1));
					then_hp = (then_hp * -4);
					change_char_health(8,then_hp);
					put_boom_on_space(24,31,5,0);
					force_instant_terrain_redraw();
					run_animation_sound(52);
				}
				if(trgt == 3){
					force_view_center(17,24);
					force_instant_terrain_redraw();
					pause(3);
					set_terrain(17,24,0);
					activate_hidden_group(4);
					relocate_character(9,17,24);
					then_hp = (get_flag(93,1));
					then_hp = (then_hp * -4);
					change_char_health(9,then_hp);
					put_effect_on_space(17,24,13,4,1);
					force_instant_terrain_redraw();
					run_animation_sound(17);
				}
				if(trgt == 4){
					force_view_center(31,24);
					force_instant_terrain_redraw();
					pause(3);
					set_terrain(31,24,0);
					activate_hidden_group(5);
					relocate_character(10,31,24);
					then_hp = (get_flag(94,1));
					then_hp = (then_hp * -4);
					change_char_health(10,then_hp);
					put_boom_on_space(31,24,7,0);
					force_instant_terrain_redraw();
					run_animation_sound(71);
				}
				pause(3);
				end();
			}
			else{
				which_atk = get_ran(1,1,10);
			}
		}

		// FIREBLAST MkII
		if((which_atk >= 1) && (which_atk <= 4)){
			print_str_color("The Immortal casts Fireblast MkII.",1);
			set_character_pose(my_number(),1);
			force_instant_terrain_redraw();
			pause(2);

			trgt = 0;
			while(trgt <= 119){
				if((char_ok(trgt) == TRUE) && (char_attitude_to_char(my_number(),trgt) == 2)){
					rctx = char_loc_x(trgt);
					rcty = char_loc_y(trgt);
					put_straight_zap(mctx,mcty,rctx, rcty,0);
					put_boom_on_space(rctx,rcty,0,0);
					put_effect_on_space(rctx,rcty,9,4, 2);
					run_animation_sound(51);
					firedmg = get_ran(1,100,300);
					if(((get_char_status(trgt,8) > 0) || (get_char_status(trgt,10) > 0)) || ((get_char_status(trgt,11) > 0) || (get_char_status(trgt,18) > 0)))
						firedmg = (firedmg - get_ran(get_stat(trgt,39),0,15));
					if(firedmg < 50)
						firedmg = 50;
					damage_char(trgt,firedmg,1);
				}

				trgt = (trgt + 1);
			}
		}

		// RED HAZE
		if((which_atk >= 5) && (which_atk <= 8)){
			print_str_color("The Immortal casts Red Haze.",1);
			set_character_pose(my_number(),1);
			force_instant_terrain_redraw();
			pause(2);

			trgt = 0;
			while(trgt <= 119){
				if((char_ok(trgt) == TRUE) && (char_attitude_to_char(my_number(),trgt) == 2)){
					rctx = char_loc_x(trgt);
					rcty = char_loc_y(trgt);
					put_sparkles_on_space(rctx,rcty,7, 8);
					run_animation_sound(51);
					then_hp = get_health(trgt);
					firedmg = get_ran(1,50,100);
					if(((get_char_status(trgt,8) > 0) || (get_char_status(trgt,10) > 0)) || ((get_char_status(trgt,11) > 0) || (get_char_status(trgt,18) > 0)))
						firedmg = (firedmg - get_ran(get_stat(trgt,39),0,15));
					damage_char(trgt,firedmg,1);
					now_hp = get_health(trgt);
					if(now_hp < then_hp)
						set_char_status(trgt,18, get_ran(1,4,8),0,0);
				}

				trgt = (trgt + 1);
			}
		}

		// RUINATION
		if((which_atk >= 9) && (which_atk <= 10)){
			print_str_color("The Immortal casts Ruination.",1);
			set_character_pose(my_number(),1);
			force_instant_terrain_redraw();
			pause(2);

			if(get_ran(1,0,3) == 3){
				count = 0;
				trgt = get_ran(1,0,3);
				while((count <= 3) && (which_atk < 11)){
					if(char_ok(trgt) == TRUE){
						which_atk = 11;
					}

					if(which_atk < 11){
						count = (count + 1);
						trgt = (trgt + 1);
						if(trgt > 3)
							trgt = 0;
					}
				}
			}

			if(which_atk < 11){
				trgt = get_ran(1,0,119);
				count = 0;
			}
			while((count <= 119) && (which_atk < 11)){
				if((char_ok(trgt) == TRUE) && (char_attitude_to_char(my_number(),trgt) == 2)){
					which_atk = 11;
				}
				if(which_atk < 11){
					count = (count + 1);
					trgt = (trgt + 1);
					if(trgt >= 120)
						trgt = 0;
				}
			}
			if(which_atk < 11){
				last_abil_time = get_current_tick();
				end();
			}

			rctx = char_loc_x(trgt);
			rcty = char_loc_y(trgt);
			put_straight_zap(24,16,rctx,rcty,0);
			put_straight_zap(24,18,rctx,rcty,0);
			put_straight_zap(23,17,rctx,rcty,0);
			put_straight_zap(25,17,rctx,rcty,0);
			put_straight_zap(24,17,rctx,rcty,0);
			put_boom_on_space(rctx,rcty,1,8);
			put_boom_on_space(rctx,rcty,1,8);
			put_boom_on_space(rctx,rcty,1,8);
			put_boom_on_space(rctx,rcty,1,8);
			put_boom_on_space(rctx,rcty,1,8);
			put_boom_on_space(rctx,rcty,1,8);
			put_boom_on_space(rctx,rcty,1,8);
			put_boom_on_space(rctx,rcty,1,8);
			play_sound(169);
			run_animation_sound(152);
			firedmg = 5;
			if(((get_char_status(trgt,8) > 0) || (get_char_status(trgt,10) > 0)) || ((get_char_status(trgt,11) > 0) || (get_char_status(trgt,18) > 0)))
				firedmg = (firedmg - get_ran(get_stat(trgt,39),0,(get_ran(1,0,2))));
			set_char_status(trgt,8,firedmg,0,0);
			firedmg = get_ran(1,400,500);
			if(((get_char_status(trgt,8) > 0) || (get_char_status(trgt,10) > 0)) || ((get_char_status(trgt,11) > 0) || (get_char_status(trgt,18) > 0)))
				firedmg = (firedmg - get_ran(get_stat(trgt,39),0,(get_ran(1,0,100))));
			damage_char(trgt,firedmg,1);
		}
		set_character_pose(my_number(),0);
		force_instant_terrain_redraw();
		pause(4);

		if(get_ran(1,0,5) < 5)
			last_abil_time = get_current_tick();
	}

break;

beginstate TALKING_STATE;
	if (get_memory_cell(3) == 0) {
		print_str("Talking: It doesn't respond.");
		end();
		}
	begin_talk_mode(get_memory_cell(3));
break;